package com.game.dao;

import com.game.entity.User;
import com.game.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserDao {

    public int login(User user) throws Exception {
        String sql = "select * from tb_user where name = ? and password = ? ;";
        Connection conn = DBUtil.getConnection();
        PreparedStatement pstat = conn.prepareStatement(sql);
        pstat.setObject(1,user.getName());
        pstat.setObject(2,user.getPassword());
        ResultSet res = pstat.executeQuery();
        List list = new ArrayList<>();
        while(res.next()){
            User suser = new User();
            suser.setId((Integer) res.getObject(1));
            suser.setName((String) res.getObject(2));
            suser.setPassword((String) res.getObject(3));
            list.add(suser);
        }
        DBUtil.close(conn,pstat,res);// 关闭数据库的流
        return list.size();
    }
}
